﻿/*
 * 测试人员看到的自己的任务列表界面.
 * 090824
 * 
 * */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Model;
using EQT.Dal;
using EQT.Bll;

public partial class MyTask : System.Web.UI.Page
{
    #region 数据访问成员
    private DaDept dalDept = new DaDept(DaCommon.DefaultDb);
    private DaEmployee daemp = new DaEmployee(DaCommon.DefaultDb);
    private DaReportAudit dara = new DaReportAudit(DaCommon.DefaultDb);
    private DaCoTestItem dacti = new DaCoTestItem(DaCommon.DefaultDb);
    private DaNotice danotice = new DaNotice(DaCommon.DefaultDb);
    private DaTuser datu = new DaTuser(DaCommon.DefaultDb);
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //绑定合同状态下拉列表
            Utility.BindListCtl2Dic(DropDownListStatus, ContractStatusCollection.TesterGrpStatus);
            InitDefaultFitler();
            LoadTaskList();
        }
    }
    #region 查询绑定相关函数
    private void InitDefaultFitler()
    {
        DateTime default_begin_day = new DateTime(DateTime.Today.Year, 1, 1);
        TextBoxBegin.Text = default_begin_day.ToString(SiteConfig.DateFormat);
        TextBoxEnd.Text = DateTime.Now.AddDays(1).ToString(SiteConfig.DateFormat);
        DropDownListStatus.SelectedIndex = DropDownListStatus.Items.IndexOf(DropDownListStatus.Items.FindByValue(ContractStatus.ALL.ToString()));
        TextBoxReportId.Text = String.Empty;
        TextBoxCorpName.Text = String.Empty;
    }

    /// <summary>
    /// 加载任务列表
    /// </summary>
    private void LoadTaskList()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //时间范围是必须的
        sb.Append(DaCoTestItem.SQL_GET_CO_TEST_ITEM);
        sb.Append(" WHERE ");
        sb.Append(Utility.GenerateQueryFilter2DateTime("Report_Create_Time", TextBoxBegin.Text, TextBoxEnd.Text));
        string filter = Utility.GenerateQueryFilterString("Report_ID", TextBoxReportId.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);
        filter = Utility.GenerateQueryFilterString("Corp_Name", TextBoxCorpName.Text, 2);
        if (filter != null) sb.AppendFormat(" AND {0} ", filter);

        sb.AppendFormat(" AND {0}", Utility.GenerateQueryFilterIntMaskAnd("Report_Status", "&", Int32.Parse(DropDownListStatus.SelectedValue)));
        string sql = sb.ToString();


        if (Utility.GlobalLog.IsDebugEnabled) Utility.GlobalLog.Debug(sql);
        int totalCount = 0;
        DataSet ds = DaCommon.GetPaginationData(sql,
            SiteConfig.PageSize, AspNetPager1.CurrentPageIndex - 1, "Report_Create_Time DESC", out totalCount);
        AspNetPager1.PageSize = SiteConfig.PageSize;
        AspNetPager1.RecordCount = totalCount;
        GridViewTestItems.DataSource = ds;
        GridViewTestItems.DataBind();
    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        LoadTaskList();
    }
    protected void btnFilterQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        LoadTaskList();
    }
    protected void btnQueryAll_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 0;
        InitDefaultFitler();
        LoadTaskList();
    }
    #endregion
    /// <summary>
    /// 下发一个任务.下发一个任务是弹出本组成员，选择人员下发
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnAssignCoTestItem_Click(object sender, EventArgs e)
    {
        //刷新列表
        LoadTaskList();
    }
    #region 对任务进行操作的各个按钮事件
    /// <summary>
    /// 退回一个任务
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnUntread_Click(object sender, EventArgs e)
    {
        //退回一个任务会将任务置为【已退回】状态，但其他信息不修改，同时执行退回的人，发一条定向消息给
        //组长(分配人)
        //有一个人退回，此任务就变为【已退回】
        Button btn = sender as Button;
        if (btn != null)
        {
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            //合同检验项主键
            string co_item_id = (string)GridViewTestItems.DataKeys[gvr.RowIndex].Value;
            MoCoTestItem mocti = dacti.GetEntity(co_item_id);

            UserContext context = Session["USER_CONTEXT"] as UserContext;
            if (dacti.UpdateCoTestItemState(co_item_id, context.UserID, CoTestItemStatus.Untread) > 0)
            {               

                //发消息给组长
                if (String.IsNullOrEmpty(mocti.Charge_Master) == false)
                {
                    MoNotice n = new MoNotice();
                    n.Notice_Id = SequenceKeys.GenerateNoticeID();
                    n.Uid = context.UserID;
                    n.Publish_Type = 1;
                    n.Title = "检验任务退回通知";
                    n.Create_Time = DateTime.Now;
                    n.Notice_Content = String.Format("{0}:{1}[{2}]退回了合同{3}的检验任务：{4}[{5}]", DateTime.Now,
                        context.EmpName, context.EmpID, mocti.Report_Id, mocti.Item_Title, mocti.Item_Code);
                    if (danotice.CreateNotice(n, mocti.Charge_Master))
                    {                        
                        Utility.SaveNoticeContent(n.Notice_Id,n.Notice_Content);
                    }
                    
                }


            }
            //刷新列表
            LoadTaskList();
        }


    }
    /// <summary>
    /// 开始一个任务
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnStartTestItem_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        if (btn != null)
        {
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            //合同检验项主键
            string co_item_id = (string)GridViewTestItems.DataKeys[gvr.RowIndex].Value;
            MoCoTestItem mocti = dacti.GetEntity(co_item_id);

            UserContext context = Session["USER_CONTEXT"] as UserContext;
            if (dacti.UpdateCoTestItemState(co_item_id, context.UserID,  CoTestItemStatus.Testing) > 0)
            {
                //发消息给组长
                if (String.IsNullOrEmpty(mocti.Charge_Master) == false)
                {
                    MoNotice n = new MoNotice();
                    n.Notice_Id = SequenceKeys.GenerateNoticeID();
                    n.Uid = context.UserID;
                    n.Publish_Type = 1;
                    n.Title = "检验任务开始执行通知";
                    n.Create_Time = DateTime.Now;
                    n.Notice_Content = String.Format("{0}:{1}[{2}]开始执行合同{3}的检验任务：{4}[{5}]", DateTime.Now,
                        context.EmpName, context.EmpID, mocti.Report_Id, mocti.Item_Title, mocti.Item_Code);
                    if (danotice.CreateNotice(n, mocti.Charge_Master))
                    {
                        Utility.SaveNoticeContent(n.Notice_Id, n.Notice_Content);
                    }

                }


            }
            //刷新列表
            LoadTaskList();
        }
    }
    /// <summary>
    /// 完成检验项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnEndTestItem_Click(object sender, EventArgs e)
    {
        //置某个检验项的状态【任务状态】为【试验完成】，即组员完成
        //退回一个任务会将任务置为【已退回】状态，但其他信息不修改，同时执行退回的人，发一条定向消息给
        //组长(分配人)
        Button btn = sender as Button;
        if (btn != null)
        {
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            //合同检验项主键
            string co_item_id = (string)GridViewTestItems.DataKeys[gvr.RowIndex].Value;
            MoCoTestItem mocti = dacti.GetEntity(co_item_id);
            UserContext context = Session["USER_CONTEXT"] as UserContext;
            if (dacti.UpdateCoTestItemState(co_item_id, context.UserID,  CoTestItemStatus.TestDone) > 0)
            {
                //发消息给组长
                if (String.IsNullOrEmpty(mocti.Charge_Master) == false)
                {
                    MoNotice n = new MoNotice();
                    n.Notice_Id = SequenceKeys.GenerateNoticeID();
                    n.Uid = context.UserID;
                    n.Publish_Type = 1;
                    n.Title = "检验任务完成通知";
                    n.Create_Time = DateTime.Now;
                    n.Notice_Content = String.Format("{0}:{1}[{2}]完成了合同{3}的检验项，：{4}[{5}],请尽快确认。", DateTime.Now,
                        context.EmpName, context.EmpID, mocti.Report_Id, mocti.Item_Title, mocti.Item_Code);
                    if (danotice.CreateNotice(n, mocti.Charge_Master))
                    {
                        Utility.SaveNoticeContent(n.Notice_Id, n.Notice_Content);
                    }

                }


            }
            //刷新列表
            LoadTaskList();
        }

    }
    #endregion

    /// <summary>
    /// 绑定待检验项目时，为【分派人员】按钮添加弹出窗口的脚本。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewTestItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button BtnUntread = e.Row.FindControl("BtnUntread") as Button;
            Button BtnStartTestItem = e.Row.FindControl("BtnStartTestItem") as Button;
            Button BtnEndTestItem = e.Row.FindControl("BtnEndTestItem") as Button;
            DataRowView drv= e.Row.DataItem as DataRowView;

            if (drv != null)
            {
                int state = (int)drv["Test_Status"];              
                BtnUntread.Enabled = (state == (int)CoTestItemStatus.Assigned); //仅当是【已分配】，退回按钮可用
                BtnStartTestItem.Enabled = (state == (int)CoTestItemStatus.Assigned);//仅当是【已分配】，开始试验按钮可用
                BtnEndTestItem.Enabled = (state == (int)CoTestItemStatus.Testing);//仅当是【测试中】，【完成】按钮才可用
            }


        }
    }
}
